package com.ruoyi.sale.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sale.domain.SaleRecord;
import com.ruoyi.sale.service.ISaleRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author h w
 * @date 2022-12-10
 */
@RestController
@RequestMapping("/sale/sale")
public class SaleRecordController extends BaseController
{
    @Autowired
    private ISaleRecordService saleRecordService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:list')")
    @GetMapping("/list")
    public TableDataInfo list(SaleRecord saleRecord)
    {
        startPage();
        List<SaleRecord> list = saleRecordService.selectSaleRecordList(saleRecord);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    public List<SaleRecord> listAll(SaleRecord saleRecord)
    {
        return saleRecordService.selectSaleRecordList(saleRecord);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SaleRecord saleRecord)
    {
        List<SaleRecord> list = saleRecordService.selectSaleRecordList(saleRecord);
        ExcelUtil<SaleRecord> util = new ExcelUtil<SaleRecord>(SaleRecord.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:query')")
    @GetMapping(value = "/{saleRecordId}")
    public AjaxResult getInfo(@PathVariable("saleRecordId") Long saleRecordId)
    {
        return success(saleRecordService.selectSaleRecordBySaleRecordId(saleRecordId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SaleRecord saleRecord)
    {
        saleRecord.setCreateBy(getLoginUser().getUsername());
        int res = saleRecordService.insertSaleRecord(saleRecord);
        if(res == 0) {
            return error("终端客户余额不足或供应商余额不足，新增销售记录失败！");
        }
        return toAjax(res);
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SaleRecord saleRecord)
    {
        return toAjax(saleRecordService.updateSaleRecord(saleRecord));
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('sale:sale:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{saleRecordIds}")
    public AjaxResult remove(@PathVariable Long[] saleRecordIds)
    {
        return toAjax(saleRecordService.deleteSaleRecordBySaleRecordIds(saleRecordIds));
    }
}
